#来自Seer的拓展功能，比如截图，创建路径，等等
import os
import sys
import time
from win32com.client import Dispatch
def regsvr():
    try:
        dm_1 = Dispatch('dm.dmsoft')
        dm_1.SetShowErrorMsg(0)#关闭错误
        return dm_1
    except Exception:
        dm_path = './'
        os.system(r'regsvr32 /s %s\dm.dll' % dm_path)#需要管理员权限运行
        try:
            dm_1 = Dispatch('dm.dmsoft')
            dm_1.SetShowErrorMsg(0)#关闭错误
            return dm_1
        except Exception:
            print("错误：需要管理员权限运行！！")
            print("错误：需要管理员权限运行！！")
            print("错误：需要管理员权限运行！！")
            print("错误：需要管理员权限运行！！")
    return None


main_path = ""
dm = regsvr()
hwnd = 0
def _init():
    global main_path
    
    _path = ''
    _path = sys.argv[0]
    _path = os.path.dirname(_path)
    if _path[-8:] == 'SeerMain':
        _path = str(_path[0:-8])
    sys.path.append(_path + '\\List\\SeerNode\\examples\\example_calculator\\Screen')#Yoso路径
    sys.path.append(_path + '\\List\\SeerNode\\examples\\example_calculator\\List')#Yoso路径
    sys.path.append('./examples/example_calculator/Screen')#当前相对路径
    sys.path.append('./examples/example_calculator/List')#当前相对路径
    main_path = _path
_init()

def sethwnd(hwnd01):
    global hwnd
    hwnd = hwnd01

def Task_GetPath(num=0,str01="任务码"):
    #获取一系列路径
    path01 = main_path
    if(num == 0):
        #获取当前工作目录路径
        return path01
    elif(num == 1):
        #获取当前任务文件夹路径
        path01 = path01 + "\\List\\" + str01
        return path01
    elif(num == 2):
        #获取当前状态的配置文件路径
        path01 = path01 + "\\List\\" + str01 + "\\配置文件.ini"
        return path01
    elif(num == 3):
        #获取当前任务的配置文件路径
        path01 = path01 + "\\List\\" + str01 + "\\任务日志.ini"
        return path01
    elif(num == 6):
        #ScreenSpy截图功能路径
        path01 = path01 + "\\List\\SeerNode\\examples\\example_calculator\\Screen\\"
        return path01
    elif(num == 7):
        #ScreenSpy截图文件路径
        path01 = path01 + "\\Screen\\"
        return path01

def open_ScreenSpy():
    #str02 = Task_GetPath(7)
    str02 = os.getcwd()#截图工具会把图片放在运行目录的Screen文件夹里
    str02 = str02 + "\\Screen\\"
    #所以必须得在运行目录创建Screen文件夹
    isExists = os.path.exists(str02)#判断是否存在 任务目录，如果不存在就创建
    if(isExists == False):
        os.mkdir(str02)#创建空Screen文件夹
    
    str01 = Task_GetPath(6)
    str01 = str01 + "ScreenSpy.exe"
    os.system(str01)
    

def ScreenSpy(str01 = ""):
    #负责快捷截图的后续工作
    #自动计算位置，并填到相应的任务配置里面
    if(str01 == ""):
        return
    #str02 = Task_GetPath(7)
    str02 = os.getcwd()#截图工具会把图片放在运行目录的Screen文件夹里
    #所以必须得在运行目录删除Screen文件夹
    str02 = str02 + "\\Screen\\"
    strbmp = str02 + "WallPaper.bmp"#路径
    strDian = str02 + "Dian.txt"#路径
    strQuYu = str02 + "QuYu.txt"#路径
    
    path01 = Task_GetPath(0)#获取当前工作目录路径
    path02 = path01 + "\\List\\" + str01#获取当前任务文件夹路径
    path03 = path02 + "\\" + str01 #获取当前任务文件夹->图片的路径
    path04 = path02 + "\\配置文件.ini"##获取当前任务的配置文件路径

    isExists = os.path.exists(path02)#判断是否存在 任务目录，如果不存在就创建
    if(isExists == False):
        os.mkdir(path02)
        dm.WriteIni("","","",path04)
    
    is01 = dm.IsFileExist(strbmp)#QuYu存在吗？图存在吗？
    is02 = dm.IsFileExist(strQuYu)
    is03 = dm.IsFileExist(strDian)
    
    if(is01 == is02 == True):#然后就存储图
        for i in range(1,500):
            section = str01+"_"+str(i)#ini项名称
            num01 = i#图片序号
            Text01 = dm.ReadIni(section,"类型",path04)
            if(Text01 == ""):
                break
        dm.WriteIni(section,"类型","图片",path04)
        str02 = dm.ReadFile(strQuYu)
        str02=str02.replace("\n","")
        str02=str02.replace("\r","")
        list01 = str02.split(",")
        dm_ret02 = dm.ScreenToClient(hwnd,int(list01[0]),int(list01[1]))#屏幕坐标转换为窗口坐标
        dm_ret04 = dm.ScreenToClient(hwnd,int(list01[2]),int(list01[3]))#屏幕坐标转换为窗口坐标

        #计算需要识别该图片的区域
        if(dm_ret02[1] > dm_ret04[1]):
            x1 = dm_ret04[1]
            x2 = dm_ret02[1]
        elif(dm_ret02[1] < dm_ret04[1]):
            x2 = dm_ret04[1]
            x1 = dm_ret02[1]
        if(dm_ret02[2] > dm_ret04[2]):
            y1 = dm_ret04[2]
            y2 = dm_ret02[2]
        elif(dm_ret02[2] < dm_ret04[2]):
            y2 = dm_ret04[2]
            y1 = dm_ret02[2]
        print("x1=" + str(x1) + "," + "y1=" + str(y1))
        print("x2=" + str(x2) + "," + "y2=" + str(y2))

        str02 = str(x1)+","+str(y1)+","+str(x2)+","+str(y2)#合成识图区域字符串，准备写入文件
        dm.WriteIni(section,"区域",str02,path04)


        str06 = "_"+str(num01)+".bmp"#图片名字后缀字符串
        #dm.CopyFile(strbmp,path03+str06,1)#把截图工具的图片导入任务文件夹
        dm.ImageToBmp(strbmp,path03+str06)

        dm.WriteIni(section,"资源",section+".bmp",path04)
        #if(Fun("0004","0004_6") == 1):
        print("图像查找：" + 'if(Fun("' + str01 + '","' + section + '") == 1):')
        str11 = 'if(Fun("' + str01 + '","' + section + '") == 1):'
        dm.SetClipboard(str11)#设置剪切板
    
    if(is03 == True):#然后就存储点
        for i in range(1,500):
            section = str01+"_"+str(i)#ini项名称
            num01 = i#图片序号
            Text01 = dm.ReadIni(section,"类型",path04)
            if(Text01 == ""):
                break
        
        dm.WriteIni(section,"类型","鼠标",path04)
        
        str02 = dm.ReadFile(strDian)
        str02=str02.replace("\n","")
        str02=str02.replace("\r","")
        list01 = str02.split(",")
        dm_ret02 = dm.ScreenToClient(hwnd,int(list01[0]),int(list01[1]))#屏幕坐标转换为窗口坐标
        xy = str(dm_ret02[1]) +","+ str(dm_ret02[2])
        dm.WriteIni(section,"区域",xy,path04)
        print("鼠标点击：" + 'Fun("' + str01 + '","' + section + '")')#Fun("0005","0005_4")
        str12 = 'Fun("' + str01 + '","' + section + '")'
        dm.SetClipboard(str12)#设置剪切板
    if(is01 == is02 == True and is03 == True):
        dm.SetClipboard(str11 + "\n    " + str12)#设置剪切板
    
    dm.DeleteFile(strbmp)#删除原来的图片
    dm.DeleteFile(strQuYu)#删除区域文件
    dm.DeleteFile(strDian)#删除点文件
    
    #str02 = Task_GetPath(7)
    str02 = os.getcwd()#截图工具会把图片放在运行目录的Screen文件夹里
    #所以必须得在运行目录删除Screen文件夹
    str02 = str02 + "\\Screen\\"
    os.rmdir(str02)    #删除空Screen文件夹
    
#--------------------------------------------------------------------

def createPath(str01 = ""):
    #创建指定目录
    path01 = os.getcwd()#获取当前工作目录路径
    path02 = path01 + "\\List\\" + str01#获取当前任务文件夹路径

    isExists = os.path.exists(path02)#判断是否存在 任务目录，如果不存在就创建
    if(isExists == False):
        os.mkdir(path02)

def capture(foobar,str01):
    #给指定区域保存截图
    dm_ret01 = dm.GetClientRect(foobar)#获取准星位置
    dm.MoveWindow(foobar,0,0)#移走准星
    dm_ret = dm.FoobarClose(foobar)#关闭准星
    time.sleep(0.5)#延迟
    dm_ret04 = dm.Capture(dm_ret01[1],dm_ret01[2],dm_ret01[3],dm_ret01[4], str01)#截图并保持
    return str01
#-------------------------------------------------------------------
foobar01 = 0
foobar02 = 0
foobar03 = 0
def kaiQi(str01="0000",str02="0000_1"):
    #开启--在绑定的窗口中创建图片
    global foobar01
    global foobar02
    global foobar03
    
    path01 = Task_GetPath(1,str01)#获取任务路径
    path02 = Task_GetPath(2,str01)#获取任务配置文件路径
    
    text01 = dm.ReadIni(str02,"类型",path02)
    if(text01 == '图片'):
        text02 = dm.ReadIni(str02,"区域",path02)#获取区域
        xy = text02.split(",")#0123
        #------------------------------区域
        name2 = Task_GetPath(0) + "\\icons\\区域.bmp"
        name3 = Task_GetPath(0) + "\\icons\\区域2.bmp"
        foobar01 = dm.CreateFoobarCustom(hwnd,xy[0],xy[1],name2,"FF0000",1)#根据图片大小创建窗口
        dm_ret = dm.FoobarDrawPic(foobar01,0,0,name2,"FF0000")#给窗口附加贴图
        foobar02 = dm.CreateFoobarCustom(hwnd,int(xy[2]) - 50,int(xy[3]) - 50,name3,"FF0000",1)#根据图片大小创建窗口
        dm_ret = dm.FoobarDrawPic(foobar02,0,0,name3,"FF0000")#给窗口附加贴图
        #-------------------
        path01 = path01 + '\\' + str02 + '.bmp'
        print(path01)
        foobar03 = dm.CreateFoobarCustom(hwnd,int(xy[0]),int(xy[1]),path01,"FF0000",1)#根据图片大小创建窗口
        dm_ret = dm.FoobarDrawPic(foobar03,0,0,path01,"FF0000")#给窗口附加贴图
        dm_ret = dm.FoobarUpdate(foobar03)#刷新窗口
        dm_ret = dm.FoobarUpdate(foobar01)#刷新窗口
        dm_ret = dm.FoobarUpdate(foobar02)#刷新窗口
    elif(text01 == '鼠标'):
        text02 = dm.ReadIni(str02,"区域",path02)#获取区域
        xy = text02.split(",")#xy
        #------------------------------区域
        name2 = Task_GetPath(0) + "\\icons\\区域.bmp"
        foobar01 = dm.CreateFoobarCustom(hwnd,xy[0],xy[1],name2,"FF0000",1)#根据图片大小创建窗口
        dm_ret = dm.FoobarDrawPic(foobar01,0,0,name2,"FF0000")#给窗口附加贴图
        dm_ret = dm.FoobarUpdate(foobar01)#刷新窗口
    pass

def wanCheng(str01="0000",str02="0000_1"):
    #完成--根据坐标截图，并且覆盖之前的数据
    global foobar01
    global foobar02
    global foobar03
    #----------------
    path01 = Task_GetPath(1,str01)#获取任务路径
    path02 = Task_GetPath(2,str01)#获取任务配置文件路径
    
    text01 = dm.ReadIni(str02,"类型",path02)
    if(text01 == '图片'):
        dm_ret01 = dm.GetClientRect(foobar01)#获取框位置
        dm_ret02 = dm.ScreenToClient(hwnd,dm_ret01[1],dm_ret01[2])#屏幕坐标转换为窗口坐标
        dm_ret01 = dm.GetClientRect(foobar02)#获取框位置
        dm_ret04 = dm.ScreenToClient(hwnd,int(dm_ret01[1]) + 50,int(dm_ret01[2]) + 50)#屏幕坐标转换为窗口坐标
        #计算需要的区域
        if(dm_ret02[1] > dm_ret04[1]):
            x1 = dm_ret04[1]
            x2 = dm_ret02[1]
        elif(dm_ret02[1] < dm_ret04[1]):
            x2 = dm_ret04[1]
            x1 = dm_ret02[1]
        if(dm_ret02[2] > dm_ret04[2]):
            y1 = dm_ret04[2]
            y2 = dm_ret02[2]
        elif(dm_ret02[2] < dm_ret04[2]):
            y2 = dm_ret04[2]
            y1 = dm_ret02[2]
        print("x1=" + str(x1) + "," + "y1=" + str(y1))
        print("x2=" + str(x2) + "," + "y2=" + str(y2))
        #---------------
        path01 = Task_GetPath(1,str01)#获取任务路径
        path02 = path01 + '\\' + str02 + '.bmp'
        path03 = Task_GetPath(2,str01)#获取任务配置文件路径
        
        str03 = str(x1)+","+str(y1)+","+str(x2)+","+str(y2)#合成识图区域字符串，准备写入文件
        dm.WriteIni(str02,"区域",str03,path03)
        capture(foobar03,path02)#图片路径
        
        dm.FoobarClose(foobar01)#关闭准星
        dm.FoobarClose(foobar02)#关闭准星
    elif(text01 == '鼠标'):
        dm_ret = dm.GetClientRect(foobar01)#获得需要点击位置坐标
        dm_ret02 = dm.GetClientRect(hwnd)#获得游戏窗体坐标
        xy = str(dm_ret[1] - dm_ret02[1]) + "," + str(dm_ret[2] - dm_ret02[2])
        dm.FoobarClose(foobar01)#关闭准星
        
        path03 = Task_GetPath(2,str01)#获取任务配置文件路径
        dm.WriteIni(str02,"区域",xy,path03)
    

def Task_GetAllData(str_name = "准星.bmp"):
    #获取所有任务相关的数据，截图，坐标等
    str00 = input("截图1，鼠标2：")
    if(str00 == str(1)):
        str01 = input("请输入任务码：")
        name = str_name
        name2 = "鼠标.bmp"
        """第一步创建图片"""
        foobar01 = dm.CreateFoobarCustom(hwnd,100,100,name,"FF0000",1)#根据图片大小创建窗口
        dm_ret = dm.FoobarDrawPic(foobar01,0,0,name,"FF0000")#给窗口附加贴图
        dm_ret = dm.FoobarUpdate(foobar01)#刷新窗口

        foobar02 = dm.CreateFoobarCustom(hwnd,0,0,name2,"FF0000",1)#根据图片大小创建窗口
        dm_ret = dm.FoobarDrawPic(foobar02,0,0,name2,"FF0000")#给窗口附加贴图
        dm_ret = dm.FoobarUpdate(foobar02)#刷新窗口

        foobar03 = dm.CreateFoobarCustom(hwnd,500,500,name2,"FF0000",1)#根据图片大小创建窗口
        dm_ret = dm.FoobarDrawPic(foobar03,0,0,name2,"FF0000")#给窗口附加贴图
        dm_ret = dm.FoobarUpdate(foobar03)#刷新窗口

        #-------------------
        input("按任意键收集两点坐标：")
        dm_ret01 = dm.GetClientRect(foobar02)#获取框位置
        dm_ret02 = dm.ScreenToClient(hwnd,dm_ret01[1],dm_ret01[2])#屏幕坐标转换为窗口坐标

        dm_ret01 = dm.GetClientRect(foobar03)#获取框位置
        dm_ret04 = dm.ScreenToClient(hwnd,dm_ret01[1],dm_ret01[2])#屏幕坐标转换为窗口坐标

        #计算需要识别该图片的区域
        if(dm_ret02[1] > dm_ret04[1]):
            x1 = dm_ret04[1]
            x2 = dm_ret02[1]
        elif(dm_ret02[1] < dm_ret04[1]):
            x2 = dm_ret04[1]
            x1 = dm_ret02[1]
        if(dm_ret02[2] > dm_ret04[2]):
            y1 = dm_ret04[2]
            y2 = dm_ret02[2]
        elif(dm_ret02[2] < dm_ret04[2]):
            y2 = dm_ret04[2]
            y1 = dm_ret02[2]
        print("x1=" + str(x1) + "," + "y1=" + str(y1))
        print("x2=" + str(x2) + "," + "y2=" + str(y2))
        dm.MoveWindow(foobar02,0,0)#移走准星
        dm.FoobarClose(foobar02)#关闭准星
        dm.MoveWindow(foobar03,0,0)#移走准星
        dm.FoobarClose(foobar03)#关闭准星

        path01 = os.getcwd()#获取当前工作目录路径
        path02 = path01 + "\\Task\\" + str01#获取当前任务文件夹路径
        path03 = path02 + "\\" + str01 #获取当前任务文件夹->图片的路径
        path04 = path02 + "\\配置文件.ini"##获取当前任务的配置文件路径

        isExists = os.path.exists(path02)#判断是否存在 任务目录，如果不存在就创建
        if(isExists == False):
            os.mkdir(path02)
            dm.WriteIni("","","",path04)

        for i in range(1,500):
            section = str01+"_"+str(i)#ini项名称
            num01 = i#图片序号
            Text01 = dm.ReadIni(section,"类型",path04)
            if(Text01 == ""):
                break
        dm.WriteIni(section,"类型","图片",path04)
        str02 = str(x1)+","+str(y1)+","+str(x2)+","+str(y2)#合成识图区域字符串，准备写入文件
        dm.WriteIni(section,"区域",str02,path04)
        str03 = "_"+str(num01)+".bmp"#图片名字后缀字符串
        capture(foobar01,path03,str03)
        dm.WriteIni(section,"资源",section+".bmp",path04)
        #if(Fun("0004","0004_6") == 1):
        print("任务功能：" + 'if(Fun("' + str01 + '","' + section + '") == 1):')
    
    elif(str00 == str(2)):
        #用自创建窗口来获取需要点击的位置
        str01 = input("请输入任务码：")

        foobar02 = create_Foobar("鼠标.bmp")
        input("按任意键确认指针位置：")
        dm_ret = dm.GetClientRect(foobar02)#获得需要点击位置坐标
        dm_ret02 = dm.GetClientRect(hwnd)#获得游戏窗体坐标
        xy = str(dm_ret[1] - dm_ret02[1]) + "," + str(dm_ret[2] - dm_ret02[2])
        print("相对坐标：" + xy)
        dm.MoveWindow(foobar02,0,0)#移走准星
        dm_ret = dm.FoobarClose(foobar02)#关闭准星

        path02 = Task_GetPath(1,str(str01))
        path04 = Task_GetPath(2,str(str01))
        isExists = os.path.exists(path02)#判断是否存在 任务目录，如果不存在就创建
        if(isExists == False):
            os.mkdir(path02)
            dm.WriteIni("","","",path04)
        for i in range(1,500):
            section = str01+"_"+str(i)#ini项名称
            num01 = i#图片序号
            Text01 = dm.ReadIni(section,"类型",path04)
            if(Text01 == ""):
                break
        dm.WriteIni(section,"类型","鼠标",path04)
        dm.WriteIni(section,"区域",xy,path04)
        print("任务功能：" + 'Fun("' + str01 + '","' + section + '")')#Fun("0005","0005_4")